﻿using System;
using System.IO;
using UmengAnalyticsNet.Extension;

namespace UmengAnalyticsNet
{
    public class Token
    {
        private static string _token;

        public static string GetToken()
        {
            if (!string.IsNullOrEmpty(_token))
            {
                return _token;
            }

            lock (typeof(Token))
            {
                var result = "";

                try
                {
                    var path = PathEx.GetAppDataPath("token");
                    if (File.Exists(path))
                    {
                        result = File.ReadAllText(path);
                    }
                    else
                    {
                        var guid = Guid.NewGuid().ToString("N");
                        result = guid;
                        File.WriteAllText(path, result);
                    }

                    _token = result;
                }
                catch (Exception)
                {
                    //
                }

                return result;
            }
        }
    }
}